package dal;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import model.AccountModel;
import model.AccountModelExt;
import model.ChecklistItemModel;
import model.ChecklistItemModelExt;
import model.ChecklistModel;
import model.ChecklistModelExt;
import model.EmployeeModel;
import model.EmployeeModelExt;
import model.PhaseModel;
import model.PhaseModelExt;
import model.ProjectModel;
import model.ProjectModelExt;
import model.QuotationModel;
import model.QuotationModelExt;
import model.VenueModel;
import model.VenueModelExt;

public class DalUtil {

	// SAMPLE OF UPDATE TRANSACTION
	public static boolean sampleUpdateTransaction() throws SQLException {

		// create db connection
		Connection dbConn = ConnectionProvider.startUpdateTransaction();

		GenericDal<AccountModel, AccountModelExt> dal = new GenericDal<AccountModel, AccountModelExt>(
				new AccountModel(), dbConn);
		// TODO update DB

		GenericDal<EmployeeModel, EmployeeModelExt> _dal = new GenericDal<EmployeeModel, EmployeeModelExt>(
				new EmployeeModel(), dbConn);
		// TODO update DB

		boolean res = ConnectionProvider.commitTransaction(dbConn);
		return res;
	}

	// SAMPLE OF UPDATE TRANSACTION
	public static void sampleSelectTransaction() throws SQLException {

		// create db connection
		Connection dbConn = ConnectionProvider.startSelectTransaction();

		GenericDal<AccountModel, AccountModelExt> dal = new GenericDal<AccountModel, AccountModelExt>(
				new AccountModel(), dbConn);
		// TODO select DB

		GenericDal<EmployeeModel, EmployeeModelExt> _dal = new GenericDal<EmployeeModel, EmployeeModelExt>(
				new EmployeeModel(), dbConn);
		// TODO select DB

		ConnectionProvider.releaseTransaction(dbConn);
	}

	// SAMPLE OF UPDATE TRANSACTION
	public static void sampleShareConnection() throws Exception {

		// create db connection
		Connection dbConn = ConnectionProvider.genConnection();

		GenericDal<AccountModel, AccountModelExt> dal = new GenericDal<AccountModel, AccountModelExt>(
				new AccountModel(), dbConn);
		// TODO select DB

		GenericDal<EmployeeModel, EmployeeModelExt> _dal = new GenericDal<EmployeeModel, EmployeeModelExt>(
				new EmployeeModel(), dbConn);
		// TODO select DB

		ConnectionProvider.releaseConnection(dbConn);
	}

	public static ProjectModel loadProject(int idProject) throws Exception {
		GenericDal<ProjectModel, ProjectModelExt> dal = new GenericDal<ProjectModel, ProjectModelExt>(
				new ProjectModel());
		ProjectModel query = new ProjectModel();
		query.setId(idProject);
		ProjectModel obj = dal.select(query);
		return obj;
	}

	public static VenueModel loadVenue(int idVenue) throws Exception {
		GenericDal<VenueModel, VenueModelExt> dal = new GenericDal<VenueModel, VenueModelExt>(
				new VenueModel());
		VenueModel query = new VenueModel();
		query.setId(idVenue);
		VenueModel obj = dal.select(query);
		return obj;
	}

	public static QuotationModel loadQuotation(int idQuotation)
			throws Exception {
		GenericDal<QuotationModel, QuotationModelExt> dal = new GenericDal<QuotationModel, QuotationModelExt>(
				new QuotationModel());
		QuotationModel query = new QuotationModel();
		query.setId(idQuotation);
		QuotationModel obj = dal.select(query);
		return obj;
	}

	public static List<PhaseModel> loadPhaseByProject(int idProject)
			throws Exception {
		GenericDal<PhaseModel, PhaseModelExt> dal = new GenericDal<PhaseModel, PhaseModelExt>(
				new PhaseModel());
		PhaseModelExt query = new PhaseModelExt();
		query.setIdProject(idProject);
		List<PhaseModel> list = dal.selectList(query);
		return list;
	}

	public static ChecklistModel loadChecklist(int idChecklist)
			throws Exception {
		GenericDal<ChecklistModel, ChecklistModelExt> dal = new GenericDal<ChecklistModel, ChecklistModelExt>(
				new ChecklistModel());
		ChecklistModel query = new ChecklistModel();
		query.setId(idChecklist);
		ChecklistModel obj = dal.select(query);
		return obj;
	}

	public static List<ChecklistItemModel> loadChecklistItems(int idChecklist)
			throws Exception {
		GenericDal<ChecklistItemModel, ChecklistItemModelExt> dal = new GenericDal<ChecklistItemModel, ChecklistItemModelExt>(
				new ChecklistItemModel());
		ChecklistItemModelExt query = new ChecklistItemModelExt();
		query.setIdChecklist(idChecklist);
		List<ChecklistItemModel> list = dal.selectList(query);
		return list;
	}

	public static List<ChecklistModel> loadChecklistByProject(int idProject)
			throws Exception {
		GenericDal<ChecklistModel, ChecklistModelExt> dal = new GenericDal<ChecklistModel, ChecklistModelExt>(
				new ChecklistModel());
		ChecklistModelExt query = new ChecklistModelExt();
		query.setIdProject(idProject);
		List<ChecklistModel> list = dal.selectList(query);
		return list;
	}

}
